import {API} from '../api/index.js'
// import CryptoJS from '../units/sha1.js'

const _appId="wx1ae603b4ae8372a1"



const _getWxLogin=function(){
    let uri=window.location.href
    let redirectcode=encodeURI(uri)
    let url=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${_appId}&redirect_uri=${redirectcode}&response_type=code&scope=snsapi_base&state=123#wechat_redirect`
    // let hrefs=localStorage.getItem("WxUserInfo")
    if(localStorage.getItem("WxUserInfo")==null||localStorage.getItem("WxUserInfo")==undefined){
        window.location.href=url  
    }else{
        let hrefs=localStorage.getItem("WxUserInfo")
        if(!hrefs.access_token){
            window.location.href=url
        }
    }
}




const _WeChatLogin=function(param){
    let url=`/mobile/user/WeChatLogin`
    let res= API.post(url,param)
    return res
}



const getWxCode=function(func){
    let uri=window.location.href
    if(uri.indexOf("code")==-1){
        _getWxLogin()
    }
    let param,code
    param=new URLSearchParams(uri.split("?")[1])
    code=param.get("code")
    

    // let code="0012Uzsw05cH5d1tzQqw0z6Msw02Uzs5"
    console.log(code)
    _WeChatLogin({code:code}).then(res=>{
        console.log(res)
        // alert(JSON.stringify(res))
        if(res.errcode==1&&res.message=="授权成功"){
            localStorage.setItem("WxUserInfo",JSON.stringify(res.data))
            let Obj={}
            Obj.code=code
            Obj.time=new Date().getTime();
            localStorage.setItem("WxCode",JSON.stringify(Obj))
            func()
        }else{
            _getWxLogin()
        }
    })
    
}







/**
 * 获取jsapi_ticket
 */
const _getjsapiticket=function(p){
    // let url=`https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${p}&type=jsapi`
    let url=`/mobile/user/ajaxPromoCode`
    let res= API.post(url,{url:p})
    return res
}

/**
 * 获取N位随机字符串
 * @param {*} len 
 */
const _getrandonstring=function(len){
    len = len || 32;
    let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; 
    let maxPos = $chars.length;
    let pwd = '';
　　for (let i = 0; i < len; i++) {
 　　　　pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
　　}
　　return pwd;
}



/**
 * 获取微信上传图片
 * @param {*} param 
 */
const _getmedia=function(param){
    let url=`/mobile/user/getmedia`
    let res= API.post(url,param)
    return res
}


// const _getJSSDKKEY=function(p){
//     const _timestamp=new Date().getTime()
//     const str=(_getrandstring(16))
//     let key=`
//         jsapi_ticket=${p}&
//         noncestr=${str}&
//         timestamp=${_timestamp}&
//         url=http://market.starrymedia.vip
//     `
//     console.log("_getJSSDKKEY",key)
//     let sig=CryptoJS.SHA1(key)
//     console.log("_getJSSDKKEYxxxxxxx",sig)

//     wx.config({
//         debug: true, 
//         appId: _appId, // 必填，公众号的唯一标识
//         timestamp: _timestamp, // 必填，生成签名的时间戳
//         nonceStr: str, // 必填，生成签名的随机串
//         signature: sig,// 必填，签名，见附录1
//         jsApiList: [
//             "uploadImage","chooseImage","chooseWXPay"
//         ]
//     });
// }



// const _getaccess_token=fucntion() {
//     // let url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+''+"&secret=APPSECRET"
// }






// https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html


export {
    _getWxLogin,
    _WeChatLogin,
    _getjsapiticket,
    _getmedia,
    getWxCode
}